using Godot;
using System;
using System.Collections.Generic;

public partial class BattleManager : Node2D
{
    readonly List<string> attackWords = new() { "受死！！！", "进攻！！！", "吃我一招！", };

    string GetAttackWord()
    {
        return attackWords[random.RandiRange(0, attackWords.Count - 1)];
    }

    readonly List<string> hurtWords = new() { "可恶！！", "哎呀！！", "啊~~~，好疼！！！", "走开呀！" };

    string GetHurtWord()
    {
        return hurtWords[random.RandiRange(0, hurtWords.Count - 1)];
    }

    readonly List<string> defWords = new() { "真没用！", "你就这点本事？", "没门！！！", "哈，一点也不疼！" };
    string GetDefWord()
    {
        return defWords[random.RandiRange(0, defWords.Count - 1)];
    }

    readonly List<string> beatBackWords = new() { "反击！！！", "别跑！！！", "你也别想好过！！！" };

    string GetBeatBackWord()
    {
        return beatBackWords[random.RandiRange(0, beatBackWords.Count - 1)];
    }

    readonly List<string> beatNotWords = new() { "算你走运！！！", "没有下次了！！！", "你还是别想了！！！", "难受，想哭" };
    string GetBeatNotWord()
    {
        return beatNotWords[random.RandiRange(0, beatNotWords.Count - 1)];
    }

    readonly List<string> reAttackWords = new() { "再来一次！！！", "你太慢了！！！", "", "" };
    string GetReAttackWord()
    {
        return reAttackWords[random.RandiRange(0, reAttackWords.Count - 1)];
    }

    void UpdateHPBar(Robot r)
    {
        if (r.IsPlayer || r.IsAlly)
        {
            var health = Mathf.Max(r.Entity.CurrentHealth, 0);
            healthRight.Text = health.ToString();
            progressBarRight.Value = (float)(health / (float)r.Entity.TotalHealth);
        }
        else
        {
            var health = Mathf.Max(r.Entity.CurrentHealth, 0);
            healthLeft.Text = health.ToString();
            progressBarLeft.Value = (float)(health / (float)r.Entity.TotalHealth);
        }

    }

    int CalcHitRate(Robot attacker, Robot target, WeaponEntity weapon)
    {
        int rate;
        if (attacker.Entity.ExtraProperty.MustHit > 0)
        {
            rate = 100;
        }
        else
        {
            // 攻方速度+武器命中-敌方速度
            rate = attacker.Entity.TotalSpeed + weapon.HitRate - target.Entity.TotalSpeed;
            // 这个命中可能是装备或者buff提升的
            rate += attacker.Entity.TotalHitRate;
            // 每远离一枚，命中-10%
            var p = (11 - attacker.DistanceTo(target, mapManager)) * 0.1;
            p = Mathf.Clamp(p, 0, 1);
            rate = (int)(rate * p);
            // 强行加减的命中
            rate += attacker.Entity.ExtraProperty.FinalHitRate;
            // 最终只要0-100
            rate = Mathf.Clamp(rate, 0, 100);
        }
        if (target.Entity.ExtraProperty.MustDodge > 0)
        {
            rate = 0;
        }
        return rate;
    }


    void AttackInfo(Robot attacker, BattleType battle)
    {
        string info = "";
        string word = "";
        switch (battle)
        {
            case BattleType.Attack:
                info = "发动攻击！";
                word = GetAttackWord();
                break;
            case BattleType.BeatBack:
                info = "进行反击！";
                word = GetBeatBackWord();
                break;
            case BattleType.ReAttack:
                info = "再次攻击！";
                word = GetReAttackWord();
                break;
        }
        labelInfo.Text = $"{attacker.Entity.RobotName}{info}";
        actorAvatar.Texture = GameUtil.GetPilotAvatar(attacker.Entity.Pilot.Avatar);
        actorName.Text = attacker.Entity.Pilot.PilotName;
        actorWords.Text = word;
    }

    void BeatNotResout(Robot r)
    {
        labelInfo.Text = $"{r.Entity.RobotName}无力反击";
        actorAvatar.Texture = GameUtil.GetPilotAvatar(r.Entity.Pilot.Avatar);
        actorName.Text = r.Entity.Pilot.PilotName;
        actorWords.Text = GetBeatNotWord();
    }

    void AttackResult(Robot target, int dmg)
    {
        actorAvatar.Texture = GameUtil.GetPilotAvatar(target.Entity.Pilot.Avatar);
        actorName.Text = target.Entity.Pilot.PilotName;
        if (dmg > 0)
        {
            labelInfo.Text = $"{target.Entity.RobotName}损坏{dmg}";
            actorWords.Text = GetHurtWord();
            // 更新血量ui
            UpdateHPBar(target);
        }
        else
        {
            labelInfo.Text = $"{target.Entity.RobotName}防御成功！";
            actorWords.Text = GetDefWord();
        }
    }

    static int ReAttackRate(Robot at, Robot tar)
    {
        // 机率为 (攻方速度 - 被攻方速度) / 被攻方速度
        var r = (at.Entity.TotalSpeed - tar.Entity.TotalSpeed) / tar.Entity.TotalSpeed * 100;
        return Mathf.Clamp(r, 0, 100);
    }

    int RandomPercent()
    {
        return random.RandiRange(0, 100);
    }

    static int CalcBattleExp(Robot atk, Robot tar)
    {
        // 等级 相差5级 ， 经验相差 100%
        // 公式 ： base * (1 + (atkLV - tarLV) * 0.2) / 5;
        var exp = tar.Entity.RewardExp;
        exp = Mathf.CeilToInt(exp * (1 + (tar.Entity.Level - atk.Entity.Level) * 0.2f) / 5);
        // 至少获得1点
        exp = Mathf.Max(exp, 1);
        return exp;
    }

    static int CalcKillExp(Robot atk, Robot tar)
    {
        var exp = tar.Entity.RewardExp * tar.Entity.Level * 0.4f;
        exp = Mathf.CeilToInt(exp * (1 + (tar.Entity.Level - atk.Entity.Level) * 0.2f));
        return (int)Mathf.Max(exp, 1);
    }

    private void BattleResoutInfo(Robot attacker, Robot target, int exp, int lv)
    {
        if (attacker.IsPlayer)
        {
            string lvInfo = lv > attacker.Entity.Level ? ",等级提升到" + lv : "";
            labelInfo.Text = $"{attacker.Entity.RobotName}获得{exp}点经验\r\n{lvInfo}";
        }
        else
        {
            if (target.IsDeath)
            {
                labelInfo.Text = $"{attacker.Entity.RobotName}阵亡了！";
            }
        }
    }
}